/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mysweethome.dao;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import mysweethome.entities.GeneralInfo;


/**
 *
 * @author johan
 */
public class GeneralInfoDAO extends AbstractBaseDAO<GeneralInfo> {

    private Query query;


    public GeneralInfoDAO(EntityManager em) {
        super(em);
    }

    /**
     * get general info object by id
     * @param id - id of property info
     * @return object or null if cannot find
     */
    public GeneralInfo findByPropertyInfoID(String id) {
        query = em.createNamedQuery("GeneralInfo.findByPropertyInfoID");
        query.setParameter("propertyInfoID", id);
        em.getTransaction().begin();
        GeneralInfo genaralInfo = (GeneralInfo) query.getSingleResult();
        em.getTransaction().commit();
        return genaralInfo;
    }

    /**
     * Get all general info
     * @return list of General Info, if not exist return null
     */
    public List<GeneralInfo> findAll() {
        query = em.createNamedQuery("GeneralInfo.findAll");
        em.getTransaction().begin();
        List<GeneralInfo> list = query.getResultList();        
        em.getTransaction().commit();
        return list;
    }

    /**
     * list general info for sale
     * @return list of General Info, if not exist return null
     */
    public List<GeneralInfo> findForSale(){
        query = em.createNamedQuery("GeneralInfo.findByIsForSale");
        em.getTransaction().begin();
        List<GeneralInfo> list = query.getResultList();
        em.getTransaction().commit();
        return list;
    }

     /**
     * list general info for rent
     * @return list of General Info, if not exist return null
     */
    public List<GeneralInfo> findForRent(){
        query = em.createNamedQuery("GeneralInfo.findByIsForRent");
        em.getTransaction().begin();
        List<GeneralInfo> list = query.getResultList();
        em.getTransaction().commit();
        return list;
    }

    /**
     * list general info for rent as paying guest
     * @return list of General Info, if not exist return null
     */
    public List<GeneralInfo> findForRentAsPayingGuest(){
        query = em.createNamedQuery("GeneralInfo.findByIsForRentPG");
        em.getTransaction().begin();
        List<GeneralInfo> list = query.getResultList();
        em.getTransaction().commit();
        return list;
    }

    public List<GeneralInfo> findForSaleWithDetail(double minPrice, double maxPrice, float squareFootage, String city, String location, int noOfBeds, int noOfBaths, String typeId ){
       //query = em.createQuery("SELECT g FROM GeneralInfo g WHERE g.isForSale AND g.detailPropertyInfo.price BETWEEN :minprice AND :maxprice AND g.city LIKE ")
        return null;
    }

    public List<GeneralInfo> findByCity(String city){
        query = em.createQuery("SELECT g FROM GeneralInfo g WHERE g.city LIKE :city");
        query.setParameter("city", "%"+city+"%");
         em.getTransaction().begin();
        List<GeneralInfo> list = query.getResultList();
        em.getTransaction().commit();
        return list;

    }


}
